package com.hengtiansoft.lfy.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.daimajia.swipe.SimpleSwipeListener;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.hengtiansoft.lfy.R;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * Created by daiye on 2016/10/24 0024.
 */

public class HistoryTranslateAdapter extends BaseSwipeAdapter {
    private Activity mContext = null;
    private ArrayList<HashMap<String, Object>> mList = new ArrayList<HashMap<String, Object>>();

    public HistoryTranslateAdapter(Activity context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public HashMap<String, Object> getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setData(ArrayList<HashMap<String, Object>> list) {
        this.mList = list;
        closeAllItems();
        notifyDataSetChanged();
    }

    public void clearData(){
        mList.clear();
        closeAllItems();
        notifyDataSetChanged();
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe;
    }

    @Override
    public View generateView(final int position, ViewGroup parent) {
        View convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_history_item, null);
        return convertView;
    }

    @Override
    public void fillValues(final int position, View convertView) {
        if (convertView == null)
            return;
        final SwipeLayout swipeLayout = (SwipeLayout) convertView.findViewById(getSwipeLayoutResourceId(position));
        swipeLayout.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(SwipeLayout layout) {

            }
        });
        swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
            @Override
            public void onDoubleClick(SwipeLayout layout, boolean surface) {

            }
        });
        convertView.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                swipeLayout.close();
                if (mListener != null)
                    mListener.onDeleteClick(HistoryTranslateAdapter.this, position );
            }
        });

        HashMap<String, Object> item = getItem(position);
        TextView title_text_tv = (TextView) convertView.findViewById(R.id.history_text_tv);
        TextView title_translate_tv = (TextView) convertView.findViewById(R.id.history_translate_tv);
        ImageView collect_iv = (ImageView) convertView.findViewById(R.id.collect_iv);
        collect_iv.setTag(position);
        collect_iv.setOnClickListener(mOnClickListener);

        if ( item != null) {
            title_text_tv.setText(item.get("text").toString());
            title_translate_tv.setText(item.get("translation").toString());
            if (item.get("collected").toString().equals("Y")) {
                collect_iv.setBackgroundResource(R.drawable.btn_collection_selected);
            } else {
                collect_iv.setBackgroundResource(R.drawable.btn_collection);
            }
        }
    }

//    @Override
//    public View getView(final int position, View convertView, ViewGroup parent) {
//        HashMap<String, Object> item = getItem(position);
//        ViewHolder viewholder = null;
//        if (convertView == null) {
//            viewholder = new ViewHolder();
//            convertView = mContext.getLayoutInflater().inflate(R.layout.layout_history_item, parent, false);
//            viewholder.title_text_tv = (TextView) convertView.findViewById(R.id.history_text_tv);
//            viewholder.title_translate_tv = (TextView) convertView.findViewById(R.id.history_translate_tv);
//            viewholder.collect_iv = (ImageView) convertView.findViewById(R.id.collect_iv);
//            convertView.setTag(viewholder);
//            viewholder.collect_iv.setTag(position);
//        } else {
//            viewholder = (ViewHolder) convertView.getTag();
//            viewholder.collect_iv.setTag(position);
//        }
//
//        viewholder.collect_iv.setOnClickListener(mOnClickListener);
//
//        if ( item != null) {
//            viewholder.title_text_tv.setText(item.get("text").toString());
//            viewholder.title_translate_tv.setText(item.get("translation").toString());
//            if (item.get("collected").toString().equals("Y")) {
//                viewholder.collect_iv.setBackgroundResource(R.drawable.btn_collection_selected);
//            } else {
//                viewholder.collect_iv.setBackgroundResource(R.drawable.btn_collection);
//            }
//        }
//
//        return convertView;
//    }
//
//    private class ViewHolder {
//        private TextView title_text_tv;
//        private TextView title_translate_tv;
//        private ImageView collect_iv;
//    }

    private OnClickListener mListener = null;

    public void setOnClickListener(OnClickListener l) {
        mListener = l;
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mListener != null) {
                int position = (Integer) v.getTag();
                switch (v.getId()) {
                    case R.id.collect_iv:
                        mListener.onCollectClick(HistoryTranslateAdapter.this, v, position);
                        break;
                }
            }
        }
    };

    public interface OnClickListener {
        public void onCollectClick(BaseAdapter adapter, View view, int position);
        public void onDeleteClick(BaseAdapter adapter, int position);
    }
}
